package com.back.campus_back.service.impl;

import com.back.campus_back.domain.Carousel;
import com.back.campus_back.domain.User;
import com.back.campus_back.mapper.CarouselMapper;
import com.back.campus_back.service.CarouselService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-04-25
 */
@Service
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements CarouselService {

    @Autowired
    private  CarouselMapper carouselMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    //查询轮播图
    public List<Carousel> selectCarousel(){
        List<Carousel> carousels = carouselMapper.selectList(null);
        return carousels;
    }

    //更新轮播图
    @Override
    public String updateCarousel(Carousel carousel) {
        try {
            int result = carouselMapper.updateById(carousel);
            int carousel_id= carousel.getCarouselId();
            //更新缓存信息
            redisTemplate.opsForValue().set(carousel_id,result);
            if(result == 1){
                return "up_success";
            }else{
                return "up_error";
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "up_error";

    }


    @Override
    public String deleteCarousel(Integer carouselId) {
        int deleteById = carouselMapper.deleteById(carouselId);
        redisTemplate.delete(carouselId);
        if(deleteById == 1){
            return "de_success";
        }else{
            return "de_error";
        }
    }

    //根据id查询
    @Override
    public Carousel findCarouselById(Integer carouselId) {
        Carousel selectById = carouselMapper.selectById(carouselId);
        return selectById;
    }

    //分页查询，模糊查询
    @Override
    public IPage<Carousel> findByCarousel(Integer current, Integer size) {
        QueryWrapper<Carousel> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");

        Page<Carousel> page = new Page<>(current,size);
        IPage<Carousel> carouselIPage = carouselMapper.selectPage(page,wrapper);
        return carouselIPage;
    }
}
